package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.DqcCheckTaskConfigEntity;
import com.hllxd.hism.star.basic.service.model.DqcConfigEntity;
import com.hllxd.hism.star.basic.service.service.DqcConfigService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.util.List;

/**
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 * v1/user/userInfo.do
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/dqc")
public class DqcConfigController2 {

    @Autowired
    private DqcConfigService dqcConfigService;

    @RestGrpcMapping("/listDqcConfig.do")
    public NTSTATUS listDqcConfig(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            List<DqcConfigEntity> items = dqcConfigService.listDqcConfig();
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getDqcConfig.do")
    public NTSTATUS getDqcConfig(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            RequestDefine aa = params.driverObject(RequestDefine.class);
            DqcConfigEntity item = dqcConfigService.getDqcConfig(aa.id);
            result.setData(item);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/addDqcConfig.do")
    public NTSTATUS addDqcConfig(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            DqcConfigEntity aa = params.driverObject(DqcConfigEntity.class);
            Object res = dqcConfigService.setDqcConfig(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/delDqcConfig.do")
    public NTSTATUS delDqcConfig(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            RequestDefine aa = params.driverObject(RequestDefine.class);
            Object res = dqcConfigService.delDqcConfig(aa.id);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/listDqcCheckTaskConfig.do")
    public NTSTATUS listDqcCheckTaskConfig(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            List<DqcCheckTaskConfigEntity> items = dqcConfigService.listDqcCheckTaskConfig();
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/setDqcCheckTaskConfig.do")
    public NTSTATUS setDqcCheckTaskConfig(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            DqcCheckTaskConfigEntity aa = params.driverObject(DqcCheckTaskConfigEntity.class);
//            if (aa.getId() == null || aa.getId() == 0) {
//                String userName = params.userID;
//                aa.setOwnerId(userName);
//            }
            int i = dqcConfigService.setDqcCheckTaskConfig(aa);
            result.setData(i);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/setDqcCheckTaskStatus.do")
    public NTSTATUS setDqcCheckTaskStatus(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            DqcCheckTaskConfigEntity aa = params.driverObject(DqcCheckTaskConfigEntity.class);
            Long id = aa.getId();
            Integer isOpen = aa.getIsOpen();
            int i = dqcConfigService.setDqcCheckTaskStatus(id, isOpen);
            result.setData(i);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/getDqcCheckTaskConfig.do")
    public NTSTATUS getDqcCheckTaskConfig(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            DqcCheckTaskConfigEntity aa = params.driverObject(DqcCheckTaskConfigEntity.class);
            DqcCheckTaskConfigEntity item = dqcConfigService.getDqcCheckTaskConfig(aa);
            result.setData(item);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/generateCheckSql.do")
    public NTSTATUS generateCheckSql(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            DqcCheckTaskConfigEntity aa = params.driverObject(DqcCheckTaskConfigEntity.class);
            DqcCheckTaskConfigEntity item = dqcConfigService.generateCheckSql(aa, LocalDate.now().minusDays(1));
            result.setData(item);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @Data
    public static class RequestDefine {
        public Long id;
    }

}
